var prefix = "/flowLine/manage";
$(function () {
    layui.use('laydate', function(){
        var laydate = layui.laydate;
        laydate.render({
            elem: '#start'
            ,type: 'datetime'
        });

        laydate.render({
            elem: '#end'
            ,type: 'datetime'
        });
    });
   
    load();
});
$('#flowLineTable').on('load-success.bs.table', function (e, data) {
    if (data.total && !data.rows.length) {
        $('#flowLineTable').bootstrapTable('selectPage').bootstrapTable('refresh');
    }
});

function load() {
    $('#flowLineTable').bootstrapTable(
            {
                method: 'get', // 服务器数据的请求方式 get or post
                url: prefix + "/list", // 服务器数据的加载地址
                showRefresh: true,
                // showToggle : true,
                iconSize: 'outline',
                // toolbar: '#exampleToolbar',
                striped: true, // 设置为true会有隔行变色效果
                dataType: "json", // 服务器返回的数据类型
                pagination: true, // 设置为true会在底部显示分页条
                pageList:[10,20,50],
                // queryParamsType : "limit",
                // //设置为limit则会发送符合RESTFull格式的参数
                singleSelect: false, // 设置为true将禁止多选
                // contentType : "application/x-www-form-urlencoded",
                // //发送到服务器的数据编码类型
                pageSize: 10, // 如果设置了分页，每页数据条数
                pageNumber: 1, // 如果设置了分布，首页页码
                // search : true, // 是否显示搜索框
                showColumns: false, // 是否显示内容下拉框（选择显示的列）
                sidePagination: "server", // 设置在哪里进行分页，可选值为"client" 或者
                // "server"
                queryParams: function (params) {
                    return {
                        limit: params.limit,
                        offset: params.offset,
                        order: 'desc',
                        centreId: $("*[name='centreId']").val()
                    };
                },
                columns: [
                    {
                        checkbox: true
                    },
                    {
                        field: 'id', // 列字段名
                        title: 'ID' // 列标题
                    },
                    {
                        field: 'flowName',
                        title: '流水线名称'
                    },
                    {
                        field: 'carNames',
                        title: '线路名称',
                        width: '40%'
                    },
                    {
                        field: 'createTime',
                        title: '设置时间'
                    },
                    {
                        title: '操作',
                        field: 'id',
                        align: 'center',
                        formatter: function (value, row, index) {
                            var e = '<a class="btn btn-primary btn-sm ' + s_edit_h + '" href="#" mce_href="#" title="编辑" onclick="edit(\''
                                + value + '\')"><i class="fa fa-edit"></i></a> ';

                            var d = '<a class="btn btn-warning btn-sm ' + s_remove_h + '" href="#" title="删除"  mce_href="#" onclick="remove(\''
                                    + value + '\')"><i class="fa fa-remove"></i></a>';

                            var s = '&nbsp;<a class="btn btn-success btn-sm ' + s_shelf_h + '" href="#" mce_href="#" title="散货上架单" onclick="shelfList(\''
                                + value + '\')"><i class="fa fa-plus"></i>散货上架单</a> ';

                            var p = '&nbsp;<a class="btn btn-primary btn-sm ' + s_shp_h + '" href="#" title="散货商品清单"  mce_href="#" onclick="shelfProductList(\''
                                + value + '\')"><i class="fa fa-print"></i>散货商品清单</a>';
                            
                            var f = '&nbsp;<a class="btn btn-primary btn-sm ' + s_sanhuo_h + '" href="#" title="导出散货分拣单"  mce_href="#" onclick="sanhuo(\''
                            + value + '\')"><i class="fa fa-print"></i>导出散货分拣单</a>';
                            
                            return e + d +s + p+f;
                        }
                    }]
            });
}
function reLoad() {
    $('#flowLineTable').bootstrapTable('refresh');
}

function remove(id) {
    layer.confirm('确定要删除选中的记录？', {
        btn: ['确定', '取消']
    }, function () {
        $.ajax({
            url: prefix + "/remove",
            type: "post",
            data: {
                'flowLineId': id
            },
            beforeSend: function (request) {
                index = layer.load();
            },
            success: function (r) {
                if (r.code == 0) {
                    layer.close(index);
                    layer.msg(r.msg);
                    reLoad();
                } else {
                    layer.msg(r.msg);
                }
            }
        });
    })
}
function batchRemove() {
    var rows = $('#flowLineTable').bootstrapTable('getSelections'); // 返回所有选择的行，当没有选择的记录时，返回一个空数组
    if (rows.length == 0) {
        layer.msg("请选择要删除的数据");
        return;
    }
    layer.confirm("确认要删除选中的'" + rows.length + "'条数据吗?", {
        btn: ['确定', '取消']
        // 按钮
    }, function () {
        var ids = [];
        // 遍历所有选择的行数据，取每条数据对应的ID
        $.each(rows, function (i, row) {
            ids[i] = row['id'];
        });
        $.ajax({
            type: 'POST',
            data: {
                "ids": ids
            },
            url: prefix + '/batchRemove',
            success: function (r) {
                if (r.code == 0) {
                    layer.msg(r.msg);
                    reLoad();
                } else {
                    layer.msg(r.msg);
                }
            }
        });
    }, function () {
    });
}

/**
 * 散货上架单
 */
function shelfList(flowLineId) {
    var start = $('#start').val();
    var end = $('#end').val();
    if (!start || !end) {
        layer.msg("清单生成时间未选择！",{icon: 7})
        return;
    }
    layer.open({
        id:'shelfList',
        type: 2,
        title: '散货上架单',
        maxmin: true,
        shadeClose: false, // 点击遮罩关闭层
        area : [ '90%', '95%' ],
        content: prefix + '/shelfList/'+flowLineId+"/"+ start+ "/"+ end
    });
}


function add() {
    // iframe层
    layer.open({
        id:'flowLine_add',
        type: 2,
        title: '添加流水线',
        maxmin: true,
        shadeClose: false, // 点击遮罩关闭层
        area : [ '90%', '80%' ],
        content: prefix + '/add' // iframe的url
    });
}

function edit(id) {
    layer.open({
        id:'flowLine_edit',
        type : 2,
        title : '流水线编辑',
        maxmin : true,
        shadeClose : false,
        area : [ '90%', '80%' ],
        content : prefix + '/edit/' + id // iframe的url
    });
}

//导出网点标签单
function wDian() {
	var start = $.trim($("#start").val());
	if(start == '' || start == null){
		layer.msg("清单生成开始时间不能为空");
		return;
	}
	var end = $.trim($("#end").val());
	if(end == '' || end == null){
		layer.msg("清单生成结束时间不能为空");
		return;
	}
	var centreId = $("*[name='centreId']").val();
	if(centreId == '' || centreId == null){
		layer.msg("请选择商户");
		return;
	}
	window.location.href = prefix + "/exportWdian?start="+start+"&end="+end+"&centreId="+centreId;
}

function wDians() {
    var start = $.trim($("#start").val());
    if(start == '' || start == null){
        layer.msg("清单生成开始时间不能为空");
        return;
    }
    var end = $.trim($("#end").val());
    if(end == '' || end == null){
        layer.msg("清单生成结束时间不能为空");
        return;
    }
    var centreId = $("*[name='centreId']").val();
    if(centreId == '' || centreId == null){
        layer.msg("请选择商户");
        return;
    }
    window.location.href = prefix + "/exportWdians?start="+start+"&end="+end+"&centreId="+centreId;
}

//导出散货分拣单
function sanhuo(id) {
	var start = $.trim($("#start").val());
	if(start == '' || start == null){
		layer.msg("清单生成开始时间不能为空");
		return;
	}
	var end = $.trim($("#end").val());
	if(end == '' || end == null){
		layer.msg("清单生成结束时间不能为空");
		return;
	}
	var centreId = $("*[name='centreId']").val();
	/*if(centreId == '' || centreId == null){
		layer.msg("请选择商户");
		return;
	}*/
	window.location.href = prefix + "/exportSanhuo?start="+start+"&end="+end+"&centreId="+centreId+"&id="+id;
}

/**
 * 导出货号清单
 */
function exportProductCodes() {
    var start = $.trim($("#start").val());
    if(start == '' || start == null){
        layer.msg("清单生成开始时间不能为空");
        return;
    }
    var end = $.trim($("#end").val());
    if(end == '' || end == null){
        layer.msg("清单生成结束时间不能为空");
        return;
    }
    var centreId = $("*[name='centreId']").val();
    if(centreId == '' || centreId == null){
        layer.msg("请选择商户");
        return;
    }
    window.location.href = prefix + "/exportProductCodes?start="+start+"&end="+end+"&centreId="+centreId
}

/**
 * 散货商品清单
 * @param flowLineId
 */
function shelfProductList(flowLineId) {
    var start = $('#start').val();
    var end = $('#end').val();
    if (!start || !end) {
        layer.msg("清单生成时间未选择！",{icon: 7})
        return;
    }
    window.location.href = prefix + "/exportShelfProductList?start="+start+"&end="+end+"&flowLineId="+flowLineId
}

/**
 * 线路总数清单导出
 */
function exportCarTotalList() {
    var start = $.trim($("#start").val());
    if(start == '' || start == null){
        layer.msg("清单生成开始时间不能为空");
        return;
    }
    var end = $.trim($("#end").val());
    if(end == '' || end == null){
        layer.msg("清单生成结束时间不能为空");
        return;
    }
    var centreId = $("*[name='centreId']").val();
    if(centreId == '' || centreId == null){
        layer.msg("请选择商户");
        return;
    }
    window.location.href = prefix + "/exportCarTotal?start="+start+"&end="+end+"&centreId="+centreId
}